package cn.agiledata.bank.transaction.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.agiledata.bank.common.dao.BankTemplate;
import cn.agiledata.bank.common.servlet.InitForString;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.ForeDict;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.hibernateBean.CityInfo;
import cn.agiledata.bank.hibernateBean.Transaction;
import cn.agiledata.bank.transaction.form.DrawForm;
import cn.agiledata.bank.transaction.form.DrawToOtherForm;
import com.vnv.infrastructure.logic.IPBAppFactory;

/**
 * 提现到他人银行账户Action
 * 
 * @author maxianbo
 * 
 */
public class DrawToOtherAction extends DrawAction {

    protected String getActionType() {
        return Constant.ACTION_TYPE_G; // 交易类型：提现到他人银行账户
    }

    protected String getPermitType() {
        return Constant.PERMIS_TYPE_PAYMONEY; // 权限类型：付款
    }

    protected Transaction createTransaction(DrawForm drawForm) {
        DrawToOtherForm form = (DrawToOtherForm) drawForm;
        Transaction trans = super.createTransaction(drawForm);
        trans.setCardNo(form.getCreditAccountNo());
        trans.setBankSeq(new Long(form.getCreditBankId()));
        trans.setBankName(form.getCreditBankName());
        String creditCityId = form.getCreditCityId();
        if (creditCityId != null && creditCityId.length() > 0) {
            trans.setBankCitySeq(new Long(creditCityId));
        }
        String creditProvinceId = form.getCreditProvinceId();
        if (creditProvinceId != null && creditProvinceId.length() > 0) {
            trans.setBankProvSeq(new Long(creditProvinceId));
        }
        trans.setPersonName(form.getCreditName());
        trans.setPersonMail(form.getCreditEmail());
        trans.setPersonMobile(form.getCreditMobile());
        trans.setPersonPhone(form.getCreditFixedTel());
        return trans;
    }

    public ActionForward prepare(ActionMapping mapping, ActionForm actionForm,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ActionForward forward = super.prepare(mapping, actionForm, request,
                response);
        loadCreditBankList(request);
        loadProvinceList(request);
        loadIsCorpAccount(request);
        return forward;
    }

    /**
     * 判断是否企业用户
     */
    private void loadIsCorpAccount(HttpServletRequest request) {
        Account account = getCurrentAccount(request);
        Boolean corpAccount = new Boolean(Constant.ACCOUNT_TYPE_CORPORATION
                .equals(account.getAccountType()));
        request.setAttribute("corpAccount", corpAccount);
    }

    /**
     * 加载省份列表
     */
    private void loadProvinceList(HttpServletRequest request) {
        request.setAttribute("provinceList", InitForString.dataDictMap
                .get("PROVINCE_INFO"));
    }

    /**
     * 加载收款银行列表
     */
    private void loadCreditBankList(HttpServletRequest request) {
        Account account = getCurrentAccount(request);
        if (isPersonAccount(account)) {
            // 加载个人收款银行列表
            request.setAttribute("creditBankList", InitForString.dataDictMap
                    .get("BANK_INFO"));
        } else {
            // 加载企业收款银行列表
            request.setAttribute("creditBankList", InitForString.dataDictMap
                    .get("ENTERPRISE_BANK_INFO"));
        }
    }

    /**
     * 加载收款银行账户信息
     */
    private void loadCreditBank(HttpServletRequest request, DrawToOtherForm form) {
        // 收款银行
        String creditBankId = String.valueOf((form.getCreditBankId()));
        request.setAttribute("creditBank", getCommonService().getBankInfo(
                creditBankId));
        BankTemplate bankTemplate = getBankTemplate();
        // 开户行省份
        String creditProvinceId = form.getCreditProvinceId();
        CityInfo creditProvince = bankTemplate.getCityInfo(creditProvinceId);
        request.setAttribute("creditProvince", creditProvince);
        // 开户行城市
        String creditCityId = form.getCreditCityId();
        CityInfo creditCity = bankTemplate.getCityInfo(creditCityId);
        request.setAttribute("creditCity", creditCity);
    }

    public ActionForward fill(ActionMapping mapping, ActionForm actionForm,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ActionForward forward = super.fill(mapping, actionForm, request,
                response);
        DrawToOtherForm form = (DrawToOtherForm) actionForm;
        if (hasAppExceptionMessage(request)) {
            // 如果存在义务逻辑异常，重新加载收款银行列表和省份列表
            loadCreditBankList(request);
            loadProvinceList(request);
        } else {
            // 如果不存在义务逻辑异常，加载收款银行账户信息
            loadCreditBank(request, form);
        }
        return forward;
    }

    public synchronized ActionForward draw(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        ActionForward forward = super.draw(mapping, actionForm, request,
                response);
        // 把交易对方添加到预设银行账户列表里
        DrawToOtherForm drawToOtherForm = (DrawToOtherForm) actionForm;
        savePeerBankAccount(request, drawToOtherForm);

        // 如果存在义务逻辑异常，重新装载收款银行信息
        if (hasAppExceptionMessage(request)) {
            DrawToOtherForm form = (DrawToOtherForm) actionForm;
            loadCreditBank(request, form);
        }
        return forward;
    }

    private BankTemplate getBankTemplate() {
        return (BankTemplate) IPBAppFactory.getObj(ForeDict.BANK_TEMPLATE);
    }

}
